package com.hopu.v1.service.front.personal.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.v1.mapper.front.personal.FStudentLeaveMapper;
import com.hopu.v1.pojo.common.PageData;
import com.hopu.v1.pojo.verify.Approval;
import com.hopu.v1.pojo.verify.StudentLeaveApplications;
import com.hopu.v1.service.admin.verify.ApprovalService;
import com.hopu.v1.service.front.personal.FStudentLeaveService;
import com.hopu.v1.vo.LeaveStaffVo;
import com.hopu.v1.vo.StudentLeaveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YwaiX
 * @Version: 1.0
 * @Date: Created in 2024-10-11 19:27:22
 * @Description:
 */

@Service
public class FStudentLeaveServiceImpl extends ServiceImpl<FStudentLeaveMapper, StudentLeaveApplications> implements FStudentLeaveService {

	@Autowired
	private FStudentLeaveMapper fStudentLeaveMapper;
	
	@Autowired
	private ApprovalService approvalService;
	
	@Override
	public PageData<StudentLeaveVo> getStudentLeaveList(Integer page, Integer size, Integer leaveType,Integer studentId) {
		List<StudentLeaveVo> studentLeaveVoList = fStudentLeaveMapper.getStudentLeaveList((page-1)*size,size,leaveType,studentId);
		Integer total = fStudentLeaveMapper.getTotal(leaveType,studentId);
		return new PageData<>(page,size,total,studentLeaveVoList);
	}
	
	@Override
	public LeaveStaffVo getApproves(Integer studentId) {
		return fStudentLeaveMapper.getApproves(studentId);
	}
	
	@Transactional
	@Override
	public void insertStudentLeave(StudentLeaveApplications studentLeaveApplications) {
		studentLeaveApplications.setCreatedAt(LocalDateTime.now());
		baseMapper.insert(studentLeaveApplications);
		
		List<Approval> approvals = new ArrayList<>();
		Approval staff = new Approval();
		staff.setLeaveRecordId(studentLeaveApplications.getId());
		staff.setStaffId(studentLeaveApplications.getStaffId());
		staff.setApprovalStatus(0);
		Approval teacher = new Approval();
		teacher.setLeaveRecordId(studentLeaveApplications.getId());
		teacher.setStaffId(studentLeaveApplications.getTeacherId());
		teacher.setApprovalStatus(0);
		approvals.add(staff);
		approvals.add(teacher);
		
		approvalService.insertApprove(approvals);
	}
}
